package com.yunhe.tianhe.rentcars.controller;

import com.yunhe.tianhe.rentcars.dto.JSONResponse;
import com.yunhe.tianhe.rentcars.entity.permission.Account;
import com.yunhe.tianhe.rentcars.service.IndexService;
import com.yunhe.tianhe.rentcars.service.impl.IndexServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * 创建人：田轩
 * 创建时间：2017/10/11
 * 描述：
 */
@Controller
@RequestMapping("/")
public class IndexController {
@Autowired
private IndexService indexService;
    @RequestMapping
    public String index(){
        return "index";
    }

    @RequestMapping("logout")
    public String logout(HttpServletRequest req){
        req.getSession().removeAttribute("user");
        return "redirect:/login";
    }

    @RequestMapping("password")
    public String password(){
        return "password";
    }

    @RequestMapping("passwordChange")
    @ResponseBody
    public JSONResponse passwordChange(HttpServletRequest req, String password, String oldPassword){
        Account account = (Account)req.getSession().getAttribute("account");
        Boolean result = indexService.changePassword(account.getuId(), password,oldPassword);
        int code = 0;
        String message = "原密码输入错误！";
        if(result){
            if(password.equals(oldPassword)){
                message = "密码不能和原密码一致！";
            }else {
                code = 1;
                message = "密码修改成功！";
                String mobile = account.getMobile();
                String userName = account.getUserName();
                String smsCode = "SMS_107845131";
                if (null!=mobile){
                    Map map = new HashMap<>();
                    map.put("userName",userName);
                    map.put("password",password);
                    try {
                    } catch (Exception e) {
                        throw new RuntimeException(e);
                    }
                }
            }
        }
        return new JSONResponse(code,message,result);
    }

}
